package db;
import java.sql.Connection;


import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public final class  Dbconn {
    public static Connection getConnection() throws ClassNotFoundException  {
        Connection conn=null;
    
        var url = "jdbc:mysql://hision.tpddns.cn:3306/testdb";
        var user = "chenh";
        var passwd = "190930";

        try 
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,passwd);
            //Statement stmt = conn.createStatement();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return conn;

    }
    public static void main(String[] args) {
        Connection conn = null;
        try  {conn = Dbconn.getConnection();//连接数据库
            Statement stmt = conn.createStatement();//获取数据库
            //增删改的简单示例
            stmt.execute("insert into goods (gname,gsize,gremark) values('rzjuan1','ccsb','remark')");//增加数据的示范
            //stmt.execute("delete from goods where gid=1");//删除数据的示范
            stmt.execute("update goods set gname = 'ABC',gsize = '40CR',gremark='FORTEST' where gid=5");//更新数据的示范

            ResultSet rs = stmt.executeQuery("select * from goods");//获取表的数据
            //System.out.println(rs.getString(2));
            while(rs.next()) {
                System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"
                    +rs.getString(3)+"\t"+rs.getString(4));
                
            }
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("successfully created");
        
    }
}